package com.goodomen.garbage.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goodomen.garbage.common.persistence.mapper.PolicyMapper;
import com.goodomen.garbage.common.persistence.model.Policy;
import com.goodomen.garbage.web.dto.PolicyDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class PolicyService {

    @Resource
    private PolicyMapper policyMapper;

    public PageInfo<Policy> selectList(int pageNum, int pageSize, PolicyDto policyDto) {
        Policy policy = new Policy();
        BeanUtils.copyProperties(policyDto, policy);
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {policyMapper.selectList(policy);});
    }

    public Integer insert(PolicyDto policyDto) {
        try {
            Policy policy = new Policy();
            BeanUtils.copyProperties(policyDto, policy);

            return policyMapper.insertSelective(policy);
        } catch (SqlSessionException e) {
            log.error("法规政策信息新增异常 PolicyService.update()", e);
        }
        return 0;
    }

    public Integer update(PolicyDto policyDto) {
        try {
            Policy policy = new Policy();
            BeanUtils.copyProperties(policyDto, policy);

            return policyMapper.updateByPrimaryKeySelective(policy);
        } catch (SqlSessionException e) {
            log.error("法规政策信息更新异常 PolicyService.update()", e);
        }
        return 0;
    }

    public Integer delete(Long id) {
        try {
            return policyMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error("法规政策信息删除异常 PolicyService.delete()", e);
        }
        return 0;
    }
}
